package org.jdsm.flow.gxt.users.client.widgets.user;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.data.client.loader.HttpProxy;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.*;
import com.sencha.gxt.data.shared.writer.JsonWriter;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import org.jdsm.flow.gxt.users.client.UserAutoBeanFactory;

import java.util.ArrayList;
import java.util.List;

import static org.jdsm.flow.gxt.users.client.UserAutoBeanFactory.*;

/**
 * Created by IntelliJ IDEA.
 * User: mikom
 * Date: 7/5/12
 * Time: 12:45 PM
 */
public class UsersGrid implements IsWidget {

    private String fullUsers = "data/users.json";

    private PagingLoader<PagingLoadConfig, UserPagingLoadResult> loader;
    private Grid<UserAutoBeanFactory.User> grid;

    public UsersGrid(String fullUsers) {
        this.fullUsers = fullUsers;
    }

    @Override
    public Widget asWidget() {

        UserAutoBeanFactory factory = GWT.create(UserAutoBeanFactory.class);

        UserDataRecordJsonReader reader =
                new UserDataRecordJsonReader(factory);

        String path = fullUsers;

        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, path);
        HttpProxy<PagingLoadConfig> proxy = new HttpProxy<PagingLoadConfig>(builder);
        JsonWriter<PagingLoadConfig> writer = new JsonWriter<PagingLoadConfig>(factory, PagingLoadConfig.class);
        proxy.setWriter(writer);

        loader = new PagingLoader<PagingLoadConfig, UserPagingLoadResult>(proxy, reader);
        loader.useLoadConfig(factory.pagingLoadConfig().as());
        loader.setLimit(10);
        loader.setRemoteSort(true);
        UserProperties props = GWT.create(UserProperties.class);

        ListStore<UserAutoBeanFactory.User> store = new ListStore<UserAutoBeanFactory.User>(props.key());
        loader.addLoadHandler(new LoadResultListStoreBinding<PagingLoadConfig,
                User,
                UserPagingLoadResult>(store));

        ColumnConfig<UserAutoBeanFactory.User, String> columnName = new ColumnConfig<UserAutoBeanFactory.User, String>(props.name(), 200, "Name");
        ColumnConfig<UserAutoBeanFactory.User, String> columnNickName = new ColumnConfig<UserAutoBeanFactory.User, String>(props.nickName(), 200, "Nick name");
        ColumnConfig<UserAutoBeanFactory.User, String> columnHint = new ColumnConfig<UserAutoBeanFactory.User, String>(props.hint(), 400, "Hint");

        List<ColumnConfig<UserAutoBeanFactory.User, ?>> l = new ArrayList<ColumnConfig<UserAutoBeanFactory.User,?>>();
        l.add(columnName);
        l.add(columnNickName);
        l.add(columnHint);
        ColumnModel<UserAutoBeanFactory.User> cm = new ColumnModel<UserAutoBeanFactory.User>(l);

        grid = new Grid<UserAutoBeanFactory.User>(store, cm);
        grid.getView().setForceFit(true);
        grid.setLoader(loader);
        grid.setLoadMask(true);
        grid.setBorders(true);
        grid.getView().setEmptyText("No result found");


        return grid;
    }

    public Grid<UserAutoBeanFactory.User> getGrid(){
        return grid;
    }

    public PagingLoader<PagingLoadConfig, UserPagingLoadResult> getLoader() {
        return loader;
    }
}
